home *** CD-ROM | disk | FTP | other *** search
/ SGI Hot Mix 17 / Hot Mix 17.iso / HM17_SGI / research / external / callable / calltest.f < prev    next >
Encoding:
Text File  |  1997-07-08  |  4.7 KB  |  172 lines

  1. C-----------------------------------------------------------------
  2. C      Routine to print a floating point value from an IDL variable.
  3.  
  4.     SUBROUTINE PRINT_FLOAT(VPTR)
  5.  
  6. C       Declare a Fortran Record type that has a compatible form with 
  7. C       the IDL C struct IDL_VARIABLE for a floating point value.
  8. C       Note this structure contains a union which is the size of 
  9. C       the largest data type.  This structure has been padded to
  10. C       support the union.   Fortran records are not part of
  11. C       F77, but most compilers have this option.
  12.  
  13.     STRUCTURE /IDL_VARIABLE/
  14.            CHARACTER*1 TYPE 
  15.            CHARACTER*1 FLAGS 
  16.            INTEGER*4 PAD    !Pad for largest data type 
  17.            REAL*4 VALUE_F
  18.     END STRUCTURE 
  19.  
  20.     RECORD /IDL_VARIABLE/ VPTR
  21.  
  22.     WRITE(*, 10) VPTR.VALUE_F
  23.   10    FORMAT('Program total is: ', F6.2)
  24.  
  25.     RETURN
  26.  
  27.     END
  28.  
  29. C-----------------------------------------------------------------
  30. C  This function will be called when IDL is finished with the 
  31. C  array F.  
  32.  
  33.        SUBROUTINE FREE_CALLBACK(ADDR)
  34.  
  35.           INTEGER*4 ADDR
  36.  
  37.           WRITE(*,20) LOC(ADDR)
  38.   20      FORMAT ('IDL Released:', I12)
  39.  
  40.           RETURN
  41.  
  42.        END
  43.  
  44. C-----------------------------------------------------------------
  45. C   This program demonstrates how to import data from a Fortran
  46. C   program into IDL, execute IDL statements and obtain data
  47. C   from IDL variables.  
  48.  
  49.       PROGRAM CALLTEST
  50.  
  51. C   Some Fortran compilers require external definitions for IDL routines
  52.         EXTERNAL IDL_Init !$pragma C(IDL_Init)    
  53.         EXTERNAL IDL_Cleanup !$pragma C(IDL_Cleanup)    
  54.         EXTERNAL IDL_Execute !$pragma C(IDL_Execute)
  55.         EXTERNAL IDL_ExecuteStr !$pragma C(IDL_ExecuteStr)
  56.         EXTERNAL IDL_ImportNamedArray !$pragma C(IDL_ImportNamedArray)
  57.         EXTERNAL IDL_FindNamedVariable !$pragma C( IDL_FindNamedVariable )
  58.  
  59.  
  60. C   Define arguments for IDL_Init routine
  61.         INTEGER*4 ARGC
  62.         INTEGER*4 ARGV(1)
  63.         DATA ARGC, ARGV(1) /2 * 0/
  64.   
  65. C   Define IDL Definitions  for IDL_ImportNamedArray
  66.  
  67.         PARAMETER (IDL_MAX_ARRAY_DIM = 8)
  68.         PARAMETER (IDL_TYP_FLOAT = 4)
  69.  
  70.         REAL*4 F(10)
  71.         INTEGER*4 DIM(IDL_MAX_ARRAY_DIM)
  72.         DATA DIM /10, 7*0/
  73.         INTEGER*4 VAR_PTR     !Address of IDL variable
  74.         EXTERNAL FREE_CALLBACK    !Declare external routine for use as arg
  75.  
  76.         PARAMETER (MAXLEN=80)   !Maximum character string length
  77.     PARAMETER (N_ELTS=10)    !Number of elements in array F
  78.  
  79. C  Define commands to be executed by IDL
  80.  
  81.         CHARACTER*(MAXLEN) CMDS(3)
  82.         DATA CMDS /"tmp2 = total(tmp)",
  83.      &            "print, 'IDL total is ', tmp2",
  84.      &            "plot, tmp"/
  85.         INTEGER*4 CMD_ARGV(10)
  86.  
  87. C  Define widget commands to be executed by IDL
  88.  
  89.         CHARACTER*(MAXLEN) WIDGET_CMDS(5)
  90.         DATA  WIDGET_CMDS /"a = widget_base()",
  91.      &    "b = widget_button(a,val='Press When Done',xs=300,ys=200)",
  92.      &    "widget_control, /realize, a",
  93.      &    "dummy = widget_event(a)", 
  94.      &    "widget_control, /destroy, a"/
  95.  
  96.         INTEGER*4 ISTAT 
  97.  
  98. C    Null Terminate command strings and store the address
  99. C    for each command string in CMD_ARGV 
  100.  
  101.         DO I = 1, 3  
  102.            CMDS(I)(MAXLEN:MAXLEN) = CHAR(0)
  103.            CMD_ARGV(I) = LOC(CMDS(I))
  104.         ENDDO
  105.  
  106. C   Initialize floating point array, equivalent to IDL FINDGEN(10)
  107.  
  108.         DO I = 1, N_ELTS
  109.            F(I) = FLOAT(I-1)
  110.         ENDDO
  111.  
  112. C   Print address of F 
  113.  
  114.     WRITE(*,30) LOC(F)
  115.    30    FORMAT('ARRAY ADDRESS:', I12)
  116.  
  117. C   Initialize Callable IDL
  118.  
  119.         ISTAT = IDL_Init(%VAL(0), ARGC, ARGV(1))
  120.  
  121.         IF (ISTAT .EQ. 1) THEN 
  122.  
  123. C   Import the floating point array into IDL as a variable named TMP 
  124.  
  125.           CALL IDL_ImportNamedArray('TMP'//CHAR(0), %VAL(1), DIM, 
  126.      &           %VAL(IDL_TYP_FLOAT), F, FREE_CALLBACK, %VAL(0))
  127.  
  128. C   Have IDL print the value of tmp
  129.  
  130.           CALL IDL_ExecuteStr('PRINT, TMP'//CHAR(0))
  131.  
  132. C   Execute a short sequence of IDL statements from a string array 
  133.  
  134.           CALL IDL_Execute(%VAL(3), CMD_ARGV)
  135.  
  136. C   Set tmp to zero, causing IDL to release the pointer to the
  137. C   floating point array.
  138.  
  139.           CALL IDL_ExecuteStr('TMP = 0'//CHAR(0))
  140.  
  141. C   Obtain the address of the IDL variable containing the
  142. C   the floating point data 
  143.  
  144.           VAR_PTR = IDL_FindNamedVariable('TMP2'//CHAR(0), %VAL(0)) 
  145.  
  146. C   Call a Fortran routine to print the value of the IDL tmp2 variable 
  147.           CALL PRINT_FLOAT(%VAL(VAR_PTR))
  148.  
  149.  
  150. C    Null Terminate command strings and store the address
  151. C    for each command string in CMD_ARGV 
  152.  
  153.           DO I = 1, 5  
  154.              WIDGET_CMDS(I)(MAXLEN:MAXLEN) = CHAR(0)
  155.              CMD_ARGV(I) = LOC(WIDGET_CMDS(I))
  156.           ENDDO
  157.  
  158. C   Execute a small widget program.  Pressing the button allows
  159. C   the program to end 
  160.  
  161.           CALL IDL_Execute(%VAL(5), CMD_ARGV)
  162.  
  163. C   Shut down IDL
  164.           CALL IDL_Cleanup(%VAL(0))
  165.  
  166.         ENDIF
  167.  
  168.       END
  169.     
  170.  
  171.  
  172.